package com.lh.project.service.impl;


import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.project.common.BaseResponse;
import com.lh.project.common.DateUtil;
import com.lh.project.common.ResultUtils;
import com.lh.project.common.UserHolder;
import com.lh.project.mapper.CommentReplyMapper;
import com.lh.project.model.entity.CommentRel;
import com.lh.project.model.entity.CommentReply;
import com.lh.project.model.enums.RedisConstants;
import com.lh.project.service.CommentRelService;
import com.lh.project.service.CommentReplyService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, CommentReply>
implements CommentReplyService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private CommentRelService commentRelService;


    @Resource
    private CommentReplyMapper commentReplyMapper;

    @Override
    public List<CommentReply> getByIds(List<Long> commentReplyList, Date date) {
        if(commentReplyList.size()==0){
            return null;
        }
        String str = "(";
        for(Long x : commentReplyList){
            str+=x+",";
        }
        str = str.substring(0,str.length()-1);
        str+=")";
//        return commentReplyMapper.getByIds(commentReplyList);
        List<CommentReply> commentReplyList1 = commentReplyMapper.getByIds(str);
        if(commentReplyList1==null || commentReplyList1.size()==0){
            return null;
        }
        for(int i = 0;i < commentReplyList1.size();i++){
            String spaceTime =
                    DateUtil.getDateRange(commentReplyList1.get(i).getCreateTime(), date);
            commentReplyList1.get(i).setSpaceTime(spaceTime);
            //判断该用户是否点赞
            String key = RedisConstants.TWO_COMMENT_LIKED_KEY+commentReplyList1.get(i).getId();
            if(UserHolder.getUser()!=null){
                Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, UserHolder.getUser().getId().toString());
                if(isMember){
                    System.out.println("用户点赞了嘛？"+isMember);
                    commentReplyList1.get(i).setIsLike(true);
                }else{
                    commentReplyList1.get(i).setIsLike(false);
                }
            }else{
                commentReplyList1.get(i).setIsLike(false);
            }
        }
        return commentReplyList1;
    }

    @Override
    public Boolean createCommentReply(CommentReply commentReply) {
        Long oneCommentUserId = commentReply.getOneCommentUserId();
        commentReply.setOneCommentUserId(null);
        //保存二级评论
        boolean save1 = save(commentReply);
        System.out.println("新生成的二级评论："+commentReply);
        //生成映射数据
        CommentRel commentRel = new CommentRel();
        commentRel.setParentId(oneCommentUserId);
        commentRel.setChildId(commentReply.getId());
        boolean save2 = commentRelService.save(commentRel);
        return save1&&save2;
    }

    @Override
    public BaseResponse<String> likeComment(Long id, Long commentId) {
        String key = RedisConstants.TWO_COMMENT_LIKED_KEY+commentId;
        Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, id.toString());
        if(BooleanUtil.isTrue(isMember)){
            //减数据库
            boolean isSuccess = update().setSql("comment_like = comment_like-1").eq("id", commentId).update();
            System.out.println("不点赞："+isSuccess);
            if(isSuccess){
                stringRedisTemplate.opsForSet().remove(key,id.toString());
            }
            return ResultUtils.success("取消成功");
        }else{
            boolean isSuccess = update().setSql("comment_like = comment_like+1").eq("id", commentId).update();
            System.out.println("点赞："+isSuccess);
            if(isSuccess){
                stringRedisTemplate.opsForSet().add(key,id.toString());
            }
            return ResultUtils.success("点赞成功");
        }
    }
}
